package cn.ecnu.paper.controller;
import cn.ecnu.common.Permission;
import cn.ecnu.paper.interceptor.Own;
import cn.ecnu.paper.service.PaperService;
import cn.ecnu.paper.vo.*;
import cn.ecnu.utils.R;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.ExecutionException;

@RestController
@RequestMapping("/paper")
public class PaperController {


    @Autowired
    private PaperService paperService;

//    @Own(permission = Permission.VIEW)
    @GetMapping("/statistic/{uid}")
    public R getPaperStatistic(@PathVariable("uid") Integer uid) {
        VisualData data = paperService.getVisualizationData(uid);
        return R.ok().put("data", data);
    }

    @GetMapping("/statistic/all")
    public R getAllStatistic() {
        VisualData data = paperService.getAllVisualizationData();
        return R.ok().put("data", data);
    }


    // TODO: @Own(permission = Permission.POST_PAPER)
    @PostMapping("/add")
    public R addPaper(@RequestBody PaperFrontVo vo) throws ExecutionException, InterruptedException {
        paperService.insertPaper(vo);
        return R.ok();
    }


    // 用户查看某一个人发布的论文
    // TODO: @Own(permission = Permission.VIEW)
    @GetMapping("/getPapersByUserId/{userId}")
    public R getPapersByUserId(@RequestParam(value = "pageNum", required = false) Integer pageNum,
                               @PathVariable("userId") Integer userId) throws ExecutionException, InterruptedException {
       ViewPaperVo papers = paperService.getPapersByUid(userId, pageNum);
       return R.ok().put("data", papers);
    }

    // 删除一篇论文
    // TODO: @Own(permission = Permission.VIEW)
    @GetMapping("/deletePaperById/{paperId}")
    public R deletePaperById(@PathVariable("paperId") Integer paperId,
                             @RequestHeader("userId") Integer loginUserId,
                             @RequestHeader("permission") Integer permission) {

        paperService.deletePaperById(paperId, loginUserId, permission);
        return R.ok();
    }

    // 根据论文id获取论文
    // @Own(permission = Permission.VIEW)
    @GetMapping("/getPaperByPaperId/{paperId}")
    public R getPaperByPaperId(@PathVariable("paperId") Integer paperId,
                               @RequestHeader("userId") Integer userId) throws ExecutionException, InterruptedException {
        PaperVo vo = paperService.getPaperByPaperId(paperId, userId);
        return R.ok().put("data", vo);
    }

    // 根据作者id获取论文
    // TODO Own
    @GetMapping("/getPapersByAuthorId/{authorId}")
    public R getPapersByAuthorId(@PathVariable("authorId") Integer authorId,
                                 @RequestParam(value = "pageNum", required = false) Integer pageNum) throws ExecutionException, InterruptedException {
        ViewPaperVo vo = paperService.getPapersByAuthorId(authorId, pageNum);
        return R.ok().put("data", vo);
    }

    // 根据研究方向的ID获取论文
    // TODO: @Own(permission = Permission.VIEW)
    @GetMapping("/getPapersByFieldId/{fieldId}")
    public R getPapersByFieldId(@PathVariable("fieldId") Integer fieldId,
                                @RequestParam(value = "pageNum", required = false) Integer pageNum) throws ExecutionException, InterruptedException {
        ViewPaperVo vo = paperService.getPapersByFieldId(fieldId, pageNum);
        return R.ok().put("data", vo);
    }

    // 修改论文
    // TODO: @Own(permission = Permission.VIEW)
    @PostMapping("/update")
    public R updatePaper(@RequestBody PaperFrontVo vo,
                         @RequestHeader("userId") Integer userId,
                         @RequestHeader("permission") Integer permission) throws ExecutionException, InterruptedException {
        paperService.updatePaper(vo, userId, permission);
        return R.ok();
    }

    // 收藏一篇论文
//   TODO: @Own(permission = Permission.VIEW)
    @PostMapping("/collect")
    public R collectPaper(@RequestBody CollectVo vo,
                          @RequestHeader("userId") Integer userId) {
        paperService.collectPaper(vo, userId);
        return R.ok();
    }

    //   TODO: @Own(permission = Permission.VIEW)
    @PostMapping("/uncollect")
    public R uncollectPaper(@RequestBody CollectVo vo,
                            @RequestHeader("userId") Integer userId) {
        paperService.uncollect(vo, userId);
        return R.ok();
    }


    @GetMapping("/collection/{uid}")
    public R getCollectionByUid(@PathVariable("uid") Integer uid,
                                @RequestParam(value = "pageNum", required = false) Integer pageNum) throws ExecutionException, InterruptedException {
        ViewPaperVo vo = paperService.getCollectionByUid(uid, pageNum);
        return R.ok().put("data", vo);
    }

    @GetMapping("/hot")
    public R getHotList() throws ExecutionException, InterruptedException {
        HotVo vo = paperService.getHotPaper();
        return R.ok().put("data", vo);
    }

    @GetMapping("/list")
    public R getPaperList(@RequestParam(value = "pageNum", required = false) Integer pageNum) throws ExecutionException, InterruptedException {
        ViewPaperVo vo = paperService.getPaperList(pageNum);
        return R.ok().put("data", vo);
    }

    @GetMapping("/all")
    public R getPaperAll() throws ExecutionException, InterruptedException {
        ViewPaperVo vo = paperService.getPaperAll();
        return R.ok().put("data", vo);
    }

    @PostMapping("/getPaperByIds")
    public R getPaperByIds(@RequestBody Ids ids) throws ExecutionException, InterruptedException {
        List<PaperVo> vo = paperService.getPaperByIds(ids.getIds());
        return R.ok().put("data", vo);
    }




}
